package com.st.emps.project.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.st.emps.department.model.DepartmentModel;
import com.st.emps.project.model.ProjectModel;
import com.st.emps.project.service.ProjectService;
import com.st.emps.util.FmtRequest;

/**
 * Servlet implementation class ProjectServlet
 */
@WebServlet("/ProjectServlet")
public class ProjectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProjectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		String action = request.getParameter("action");
		Object rs = null;
		switch (action) {
		case "add":
			rs = add(request);
			break;
		case "page":
			rs = page(request);
			break;
		case "del":
		    rs = delete(request);
		    break;
		case "upd":
		    rs = update(request);
		    break;
		case "get":
		    rs = get(request);
		    break;
		case "updDepart":
			rs = updDepart(request);
			break;
		case "list":
			rs = list(request);
		default:
			break;
		}

		FmtRequest.write(response.getWriter(), rs);

	}

	private String updDepart(HttpServletRequest request) {
		Map<String,String> fields = new HashMap<String, String>();
		fields.put("code", "code");
		fields.put("departmentCode", "codeDepart");
		ProjectModel model = FmtRequest.parseModel(request, ProjectModel.class, fields);
		return service.updateDepart(model);
	}


	private List<ProjectModel> list(HttpServletRequest request) {
		ProjectModel model = FmtRequest.parseModel(request,ProjectModel.class);
		List<ProjectModel> list = service.selectList(model);
		return list;
	}

	private ProjectService service = new ProjectService();

	private Map<String,Object> page(HttpServletRequest request) {
		Map<String,String> fields = new HashMap<String, String>();
		fields.put("code", "code");
		fields.put("name", "name");
		ProjectModel model = FmtRequest.parseModel(request, ProjectModel.class,fields);
		model.setPageIndex(Integer.parseInt(request.getParameter("pageIndex")));
		model.setPageLimit(Integer.parseInt(request.getParameter("pageLimit")));
		model.setPageOn(true);
		Map<String,Object> map = new HashMap<>();
		map.put("list", service.selectList(model));
		map.put("count",service.selectCount(model));
		return map;
	}
	

	private String delete(HttpServletRequest request) {
		ProjectModel model = new ProjectModel(request.getParameter("code"));
		return service.delete(model);
	}

	private String update(HttpServletRequest request) {
		ProjectModel model = FmtRequest.parseModel(request,ProjectModel.class);
		return service.update(model);
	}
	
	private ProjectModel get(HttpServletRequest request) {
		ProjectModel model = new ProjectModel(request.getParameter("code"));
		return service.selectModel(model);
	}
	
	private String add(HttpServletRequest request) {
		ProjectModel model = FmtRequest.parseModel(request,ProjectModel.class);
		return service.insert(model);
	}

}
